package com.pdsu.graduation_design.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.graduation_design.MainActivity;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.adapter.PreGoodsGridViewAdapter;
import com.pdsu.graduation_design.adapter.PreGridViewAdapter;
import com.pdsu.graduation_design.domain.Commodity;
import com.pdsu.graduation_design.net.PreFragmentCallBack;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.PicassoImageLoader;
import com.pdsu.graduation_design.utils.Result;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class Pre_Fragment extends Fragment {

    private View currentView;
    private TextView textView;

    //组件
    //轮播图banner

    private Banner banner;

    private List<String> images;

    private List<String> titles;

    //中间菜单menu

    private GridView gridView_menu;
    //下侧产品
    private GridView gridView_goods_item;
    //data
    private List<Commodity> commodities;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        currentView = inflater.inflate(R.layout.fragment_pre, null);

        init();
        operation();
        //addition 填充轮播图
        addition();
        //bind
        bind();
        return currentView;
    }

    private void operation() {

        textView.setText("首页");


    }

    private void init() {
        commodities=new ArrayList<>();

        textView = currentView.findViewById(R.id.bar_top_common_textview);
        banner = currentView.findViewById(R.id.pre_banner);
        images = new ArrayList<>();
        titles = new ArrayList<>();

        gridView_menu=currentView.findViewById(R.id.gv_all);

        gridView_goods_item=currentView.findViewById(R.id.gv_pre_goods_item);


    }


    private void bind() {
        //绑定Adapter
        //首页功能栏
        gridView_menu.setAdapter(new PreGridViewAdapter(currentView.getContext()));

        //首页商品栏
        getData();
    }

    private void addition() {
        /**
         * 待优化
         */
        images.add("https://img1.baidu.com/it/u=3094680643,1458880832&fm=253&fmt=auto&app=138&f=JPEG?w=660&h=380");
        images.add("https://oss.gzdaily.cn/site2/pad/pic/2021-11/20/736bdbbe-d641-4dfd-a8b4-9ffdf2adfbb6_batchwm_watermark.jpg");
        images.add("https://img8.iqilu.com/ksdimgs/2018/05/16/a7e7203375568650cb20b24bb961fa5a.jpg");
        titles.add("01");
        titles.add("02");
        titles.add("03");


        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        //picasso加载图片
        banner.setImageLoader(new PicassoImageLoader());

        banner.setBannerAnimation(Transformer.Accordion);

        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(currentView.getContext(), "click " + position, Toast.LENGTH_SHORT).show();
            }
        });

        banner.isAutoPlay(true);

        banner.setDelayTime(1500);

        banner.setImages(images);

        banner.setBannerTitles(titles);

        banner.start();


    }


    private void getData(){
        RequestParams requestParams=new RequestParams(MainActivity.URL+"/commodity/getRecommend");

        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    Result r= JsonUtil.JsonToObj(result,Result.class);
                    //获取数据成功
                    if (r.getCode().equals("200")){
                        commodities=JsonUtil.json2list(r.getData().toString(),Commodity.class);
                        gridView_goods_item.setAdapter(new PreGoodsGridViewAdapter(commodities,currentView.getContext()));

                        gridView_goods_item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent=new Intent(getContext(),MainListViewItemDetailActivity.class);
                                intent.putExtra("cid",commodities.get(position).getCid());
                                startActivity(intent);
                            }
                        });


                    }else {

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


    }

}